package com.hr.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hr.domain.dto.ClientDTO;
import com.hr.domain.po.Order;
import com.hr.domain.query.OrderPageParams;
import com.hr.domain.query.OrderPageResult;
import com.hr.domain.vo.ClientOrderVO;
import com.hr.domain.vo.OrderVO;
import com.hr.service.OrderService;
import com.hr.mapper.OrderMapper;
import com.hr.utils.UserContext;
import lombok.AllArgsConstructor;
import lombok.Builder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 86150
* @description 针对表【order(订单表)】的数据库操作Service实现
* @createDate 2025-09-29 17:15:34
*/
@Service
@AllArgsConstructor
public class OrderServiceImpl implements OrderService{

    private final OrderMapper orderMapper;


    /**
     * 分页获取订单列表
     * @return orderList
     */
    @Override
    public OrderPageResult getOrderPageList(OrderPageParams queryParams) {
        // 处理无效的日期参数
        if ("undefined".equals(queryParams.getStartDate())) {
            queryParams.setStartDate(null);
        }
        if ("undefined".equals(queryParams.getEndDate())) {
            queryParams.setEndDate(null);
        }
        //开始分页
        PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        //传递分页参数给mapper
        List<OrderVO> orderList = orderMapper.getOrderPageList(queryParams);
        //强转为Page<OrderVO>
        Page<OrderVO> page = (Page<OrderVO>) orderList;
        //封装为OrderPageResult，返回
        return new OrderPageResult(page.getTotal(), page.getResult());
    }

    /**
     * 修改订单状态
     * @param id
     * @param status
     * @return
     */
    @Override
    public String updateOrderStatus(Long id, Integer status) {


        int row = orderMapper.updateOrderStatus(id, status);
        return row > 0 ? "更新订单状态成功" : "更新订单状态失败";
    }

    /**
     * 获取单个用户所有订单信息
     * @return ClientOrderVO
     */
    @Override
    public List<ClientOrderVO> getOrderDetails(ClientDTO clientDTO) {
        //从UserContext，获取当前用户id
        Long userId = UserContext.getUserId();
        /*Long userId = 4L;*/
        clientDTO.setUserId(userId);

        return orderMapper.selectByUserId(clientDTO);
    }
}




